package com.zzhrtech.wgzx_cityzen.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.squareup.picasso.Picasso;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.BaseActivity;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.adapters.me.InfoListAdapter;
import com.zzhrtech.wgzx_cityzen.base.OnItemClickLitener;
import com.zzhrtech.wgzx_cityzen.beans.me.InfoBean;
import com.zzhrtech.wgzx_cityzen.beans.me.LoginBean;
import com.zzhrtech.wgzx_cityzen.ui.contact.JifenDhActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyInfoActivity extends BaseActivity {

    private TextView et_name,et_phone,et_account;
    private ImageView civ_head;
    private RecyclerView recy_tab;
    private String[] names= new String[]{"积分明细","兑换记录"};
    private int[] imgs = new int[]{R.mipmap.bg_info_jfmx,R.mipmap.bg_info_dhjl};
    private List<InfoBean> infoBeanList = new ArrayList<InfoBean>();
    private InfoListAdapter infoListAdapter;
    private TextView tv_jifen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        toolbar.setNavigationIcon(R.mipmap.ic_close);

        initView();

        httpGetInfo();
    }

    private void initView(){
        et_name = (TextView) findViewById(R.id.et_name);
        et_phone = (TextView) findViewById(R.id.et_phone);
        et_account = (TextView) findViewById(R.id.et_account);
        civ_head = (ImageView) findViewById(R.id.civ_head);

        tv_jifen = (TextView) findViewById(R.id.tv_jifen);
        recy_tab = (RecyclerView) findViewById(R.id.recy_tab);
        recy_tab.setLayoutManager(new GridLayoutManager(this,2));
        recy_tab.setItemAnimator(new DefaultItemAnimator());
        infoListAdapter = new InfoListAdapter();
        recy_tab.setAdapter(infoListAdapter);

        infoListAdapter.setOnItemClickLitener(new OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {

                switch (position){
                    case 0:
                        Intent intent = new Intent(MyInfoActivity.this,JfmxActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent = new Intent(MyInfoActivity.this,JifenDhActivity.class);
                        startActivity(intent);
                        break;
//                    case 2:
//                        intent = new Intent(MyInfoActivity.this,WebviewActivity.class);
//                        intent.putExtra("title","积分规则");
//                        intent.putExtra("url",HTTP_JFGZ);
//                        startActivity(intent);
//                        break;
                }
            }
        });

        for (int i = 0;i < names.length;i++){
            InfoBean bean = new InfoBean();
            bean.setName(names[i]);
            bean.setImg(imgs[i]);
//            infoBeanList.add(bean);
            infoListAdapter.addItem(bean);
        }



        Picasso.with(this)
                .load(getCache().getAsString(keyHead))
                .placeholder(R.mipmap.ic_placehoder)
                .into(civ_head);
    }

    private void httpGetInfo(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("citizen_id",getCache().getAsString(Constans.keyUid));
        GsonRequest<LoginBean> gsonRequest = new GsonRequest<LoginBean>(com.android.volley.Request.Method.POST, Constans.HTTP_USERINFO, LoginBean.class, params, new com.android.volley.Response.Listener<LoginBean>() {
            @Override
            public void onResponse(LoginBean response) {
                if (response.isSuccess()){
                    Message message = new Message();
                    message.what = 3;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                }else {
                    showToast(response.getMsg());
                }
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("result=====",error + "");
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 3){
                LoginBean.Data data = (LoginBean.Data) msg.obj;
                tv_jifen.setText("我的积分：" + data.getNum());
                et_account.setText(data.getAccount());
                et_name.setText(data.getNickname());
                et_phone.setText(data.getUsername());
            }

            return false;
        }
    });
}
